//
//  HNSH_SearchGoodsViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/24.
//

#define KSearchGoodsData        @"KSearchGoodsData"

#import "HNSH_SearchGoodsViewController.h"

//view
#import "HNSH_SearchGoodsNavView.h"
#import "HNSH_SearchGoodsHeader.h"
#import "HNSH_SearchGoodsCell.h"

//vc
#import "HNSH_GoodsListViewController.h"

@interface HNSH_SearchGoodsViewController ()

/* dataArray */
@property (nonatomic, strong) NSMutableArray *dataArray;

/* topNavView */
@property (nonatomic, strong) HNSH_SearchGoodsNavView *topNavView;

@end

static NSString *HNSH_SearchGoodsHeaderID = @"HNSH_SearchGoodsHeaderID";
static NSString *HNSH_SearchGoodsCellID = @"HNSH_SearchGoodsCellID";

@implementation HNSH_SearchGoodsViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self configCollectionView];
    
    [self addAllViews];
}

- (void)configCollectionView {
    [self.collectionView registerClass:[HNSH_SearchGoodsHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HNSH_SearchGoodsHeaderID];
    [self.collectionView registerClass:[HNSH_SearchGoodsCell class] forCellWithReuseIdentifier:HNSH_SearchGoodsCellID];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self setDefaultData];
}

//MARK: Myself Methods
- (void)addAllViews{
    [self.view addSubview:self.topNavView];
    [self.view addSubview:self.collectionView];
    
    [self setDefaultData];
}

- (void)setDefaultData{
    if (![[NSUserDefaults standardUserDefaults] objectForKey:KSearchGoodsData]) {
        [[NSUserDefaults standardUserDefaults] setObject:@[] forKey:KSearchGoodsData];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    NSArray * records = [[NSUserDefaults standardUserDefaults] objectForKey:KSearchGoodsData];
    self.dataArray = [NSMutableArray arrayWithArray:records];
    [self.collectionView reloadData];
}

//MARK: 返回
- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

//MARK: 搜索
- (void)searchWithKeyword:(NSString *)keyword{
    [self skipListVCWithKeyword:keyword];
    
    self.topNavView.clearField = YES;
    
    if ([self.dataArray containsObject:keyword]) {
        for (int i = 0; i < self.dataArray.count; i ++) {
            NSString *item = self.dataArray[i];
            if ([item isEqualToString:keyword]) {
                [self.dataArray removeObjectAtIndex:i];
                break;
            }
        }
        [self.dataArray insertObject:keyword atIndex:0];
    } else {
        [self.dataArray addObject:keyword];
    }
    [[NSUserDefaults standardUserDefaults] setObject:self.dataArray forKey:KSearchGoodsData];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self.collectionView reloadData];
}

- (void)skipListVCWithKeyword:(NSString *)keyword{
    HNSH_GoodsListViewController * vc = [HNSH_GoodsListViewController new];
    vc.searchWord = keyword;
    vc.shop_id = self.shop_id;
    vc.mcate_id = self.mcate_id;
    vc.scate_id = self.scate_id;
    vc.type = self.type;
    [self.navigationController pushViewController:vc animated:YES];
}

//MARK: 弹框确认删除
- (void)showAlertView{
    WeakSelf(weakSelf);
    [HNSH_AlertView showTitle:@"是否删除所有记录？" des:@"" buttonTitle:@"确定" buttonColor:[UIColor appGreenColor] buttonClick:^{
        [weakSelf delete];
    }];
}

//MARK: 删除历史记录
- (void)delete{
    [[NSUserDefaults standardUserDefaults] setObject:@[] forKey:KSearchGoodsData];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self setDefaultData];
}

//MARK: Getter Methods
- (HNSH_SearchGoodsNavView *)topNavView{
    if (!_topNavView) {
        _topNavView = [[HNSH_SearchGoodsNavView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight)];
        
        WeakSelf(weakSelf);
        _topNavView.backBlock = ^{
            [weakSelf back];
        };
        _topNavView.searchBlock = ^(NSString * _Nonnull keyword) {
            [weakSelf searchWithKeyword:keyword];
        };
    }
    return _topNavView;
}

//MARK: UICollectionViewDelegate Methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HNSH_SearchGoodsCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:HNSH_SearchGoodsCellID forIndexPath:indexPath];
    cell.title = self.dataArray[indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *title = self.dataArray[indexPath.row];
    CGFloat width = [title widthForFont:[UIFont systemFontOfSize:14]];
    return CGSizeMake(width+28, 26);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(12, 16, 12, 16);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 12;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 12;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        HNSH_SearchGoodsHeader * view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HNSH_SearchGoodsHeaderID forIndexPath:indexPath];
        
        WeakSelf(weakSelf);
        view.deleteBlock = ^{
            [weakSelf showAlertView];
        };
        return view;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kScreenWidth, 50);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *title = self.dataArray[indexPath.row];
    [self searchWithKeyword:title];
}

@end
